package com.mzx.javase.常用类;

import java.util.Calendar;
import java.util.Date;

/**
 * @Description:日期类Calendar
 * @Date: 2022/5/12 22:41
 * @Author: MarKing
 */
public class CalendarTest {
    public static void main(String[] args) {
        /**
         * 时间单位：
         * 1s = 1000 ms
         * 1ms = 1000 微秒
         * 1微秒 = 1000 纳秒
         */
        Date date = new Date();
        System.out.println(date.getYear());
        long timestamp = date.getTime();
        System.out.println("自1970年1月1日00:00:00所经历的时间毫秒数为："+timestamp);
        //Calendar常用的方法
        Calendar calendar = Calendar.getInstance();

        calendar.set(2022,2,22,22,22,22);//设置时间
        calendar.set(Calendar.MINUTE,50);
        System.out.println("获取到的年为："+calendar.get(Calendar.YEAR));
        System.out.println("获取到的月为："+calendar.get(Calendar.MONTH));//月份为0-11
        System.out.println("获取到的日为："+calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("获取到的时为："+calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("获取到的分为："+calendar.get(Calendar.MINUTE));
        System.out.println("获取到的秒为："+calendar.get(Calendar.SECOND));
        //Calendar转Date
        Date time = calendar.getTime();
        System.out.println(time);
        //Date转Calendar
        calendar.setTime(new Date());
        System.out.println("获取到的年为："+calendar.get(Calendar.YEAR));
        System.out.println("获取到的月为："+calendar.get(Calendar.MONTH));//月份为0-11
        System.out.println("获取到的日为："+calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("获取到的时为："+calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("获取到的分为："+calendar.get(Calendar.MINUTE));
        System.out.println("获取到的秒为："+calendar.get(Calendar.SECOND));


    }


}
